<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px">
            <el-form-item label="功能名称" prop="title">
                <el-input v-model="ruleForm.title"></el-input>
            </el-form-item>
            <el-form-item label="api" prop="resources">
                <el-input v-model="ruleForm.resources" placeholder="多个api用;连接"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :loading="loading" @click="sub('ruleForm')">确定</el-button>
                <el-button @click="onCancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { apiMenuDetail, apiMenuEdit } from "@/api/admin.js";
export default {
    name: "menu_index_edit_resources",
    props: {
        editId: {
            type: Number,
            required: true
        }
    },
    data() {
        return {
            ruleForm: {
                id: "", //父级菜单id
                title: "",
                resources: "",
                type: "PLATFORM",
            },
            rules: {
                title: [{ required: true, message: "请输入功能名称", trigger: "change" }],
                resources: [{ required: true, message: "请输入api", trigger: "change" }],
            },
            loading: false,
        }
    },
    created() {
        this.getData();
    },
    watch: {
        editId() {
            this.getData();
        }
    },
    methods: {
        getData() {
            apiMenuDetail({ id: this.editId }).then(({ data }) => {
                this.ruleForm.id = this.editId;
                this.ruleForm.title = data.title;
                this.ruleForm.resources = data.resources.toString();
            });
        },
        // 确定
        sub(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.loading = true;
                    const postData = JSON.parse(JSON.stringify(this.ruleForm))
                    postData.resources = postData.resources.split(';')
                    apiMenuEdit(postData).then((res) => {
                        this.$message({ message: "操作成功", type: "success" });
                        this.loading = false;
                        this.$emit("onSubmit");
                    }).catch((err) => {
                        this.loading = false;
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        // 取消
        onCancel() {
            this.loading = false;
            this.$emit("onCancel");
        },
        // 重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    },
}
</script>

<style></style>